package com.itheima.controller;

import com.itheima.domain.Customer;
import com.itheima.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.security.RolesAllowed;
import java.util.List;

/**
 * @version 1.0
 * @Author : wusiqi
 * @Date : 2019/3/7 20:48
 */

@Controller
@RequestMapping("/customer")
public class CustomerController {
    //自动注入
    @Autowired
    private CustomerService customerService;
    //查询全部
    @RequestMapping("/findAll")
    public String findAll(Model model){
        //customers
        //返回结果
        List<Customer> customers =customerService.findAll();
        model.addAttribute("customers",customers);
        return "list";
    }

    //根据id 查找信息 返回更改页面
    @RequestMapping("/findById")
    public String findById(Model model,String custId){
       Customer customer= customerService.findById(custId);
       model.addAttribute("customer",customer);
        return "edit";
    }

    //获得edit 页面的数据 传给后台 最后重定向 查找 把更改后的数据从新展示
    @RequestMapping("/update")
    public String update(Customer customer){
        customerService.update(customer);
        return "redirect:/customer/findAll";
    }
}
